const express = require('express')
const bijimodle = require('../model/bijimodle')
const session = require('express-session')
const usermodel = require('../model/usermodel')
const md5 = require('md5')
let router = express.Router()
let sessions = ''
router.post('/sumbmit', (req, res) => {
    let title = req.body.title
    let content = req.body.content
    bijimodle.create({
        Title: title,
        Content: content,
    })
    res.render('jump.html', { msg: '添加' })
})
router.post('/update', (req, res) => {
    let title = req.body.title
    let content = req.body.content
    let id = req.body.id
    console.log(id);
    bijimodle.updateOne({ _id: id }, {
        Title: title,
        Content: content
    }, (err, data) => {
        // console.log(err);
        // console.log(data);
    })
    //    res.send('成功')
    res.render('jump.html', { msg: '修改' })
})
router.post('/login', (req, res) => {
    let { username, password } = req.body
    usermodel.findOne({ username: username, password: md5(password) }, (err, data) => {
        if (data) {
            req.session.name = req.body.username
            res.render('jump.html', { msg: '登录' })
        } else {
            res.send('登录失败,您输入的用户名或密码有误')
        }
    })
})
router.post('/register', (req, res) => {
    let { username, password } = req.body
    usermodel.findOne({ username }, (err, data) => {
        if (data) {
            res.render('register.html', { msgg: '注册失败，用户名存在' })
        }
        else {
            usermodel.create({ username, password: md5(password) }, (err, data) => {
                res.render('jump.html', { msg: '注册' })
            })
        }
    })


})

module.exports = router